import { fuseSearchFun, getRandomElements, HostEnum, type GameJson } from "~/utils";
import { getServerCache } from "../plugins/cache";

/** 根据游戏id获取类似的48个游戏列表 */
export default defineEventHandler(async (event): Promise<GameJson[]> => {
  const { hostName = HostEnum.localhost, gameId, count = 48 } = await readBody(event);
  const { allGameJson = [] } = getServerCache().gameJsonDataSource[hostName];
  const gameDetail = allGameJson.find((item: GameJson) => item.id == gameId) || {};
  const { type } = gameDetail;
  const allFuseSearchFun = fuseSearchFun({
    list: allGameJson,
    keys: ["type"],
    value: type?.[0] || "",
  });
  return getRandomElements(allFuseSearchFun, count);
});
